package com.example.ecommercemarketing.handler.impl;

import com.example.ecommercemarketing.dto.MarketingElementsDTO;
import com.example.ecommercemarketing.dto.ProductDTO;
import com.example.ecommercemarketing.dto.UserDTO;
import com.example.ecommercemarketing.handler.MarketingElementsHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 小美优选榜处理器（运营人工精选商品）
 * 榜单规则：由资深买手"小美"每周精选20款商品
 */
public class XiaomeiChoiceHandler implements MarketingElementsHandler {

    private static final List<String> XIAOMEI_CHOICE_PRODUCTS = Arrays.asList(
            "3003", "3009", "3018", "1234", "2013", "2451" // 实际应从CMS系统获取
    );

    @Value("${marketing.xiaomeiRank.url:/rank/xiaomei}")
    private String rankPageUrl;

    @Override
    public MarketingElementsDTO handle(ProductDTO product, UserDTO user) {
        if (!XIAOMEI_CHOICE_PRODUCTS.contains(String.valueOf(product.getId()))) {
            return null;
        }

        //这里直接写死了一些测试数据，实际项目中是通过发其它应用的接口、查数据库、经过一些繁琐的业务逻辑处理来获取数据的
        MarketingElementsDTO elements = new MarketingElementsDTO();
        elements.setRecommendText(String.format("【小美推荐】%s · 资深买手精选好物", product.getName()));
        elements.setProductHighlights("平台首席选品官严选");
        elements.setMarketingImageUrl("https://cdn.example.com/ranks/xiaomei/" + product.getId() + ".jpg");
        elements.setExtraInfo(String.format("<a href='%s' target='_blank'>查看小美优选榜></a>", rankPageUrl));
        elements.setSource("xiaomei_rank");

        Random random = new Random();
        int randomTime = random.nextInt(15);
        //模拟实际业务中的耗时
        try {
            TimeUnit.MILLISECONDS.sleep(randomTime);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return elements;
    }
}